package importExport;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import processing.core.PApplet;
import processing.core.PGraphics;

public class SimpleVSSExporter extends PGraphics {
	String scriptName="ProcessingExport";
	String filePath;
	boolean fill;
	boolean stroke;
	float strokeWeight=0.1f;
	PrintStream p;
	int cFillPat=1;
	int cPenPat=1;
	public SimpleVSSExporter(int width, int height, PApplet applet, String path) {
		// super();
		/*
		 * this.width this.app
		 */
		System.out.println("VSS export started.");
		System.out.println("close with -closeVSS()-");
		File file=new File(path);
		try {
			FileOutputStream out = new FileOutputStream(file
					.getAbsolutePath());
			 p = new PrintStream(out);
			 p.println("PROCEDURE "+scriptName+";");
			 p.println("VAR");
			 p.println("hd:HANDLE;");
			 p.println("hd1:HANDLE;");
			 p.println("bl:BOOLEAN;");
			 p.println("Begin");
		} catch (Exception e) {
			System.err.println("error writing file");
		}
		
		// this.

	}
	public void vertex(float x, float y){
		p.println("addPoint("+x+","+y+");");
	}
	public void beginShape(int mode) {
		//System.out.println("beginShape");
		
		p.println("beginPoly;");
	}
	public void endShape(int close) {
		endShape();
		
	}
	public void closePoly(){
		p.println("closePoly;");
	}
	public void openPoly(){
		p.println("openPoly;");
	}
	public void endShape() {
		p.println("endPoly;");
	}
	public void line(float x1,float y1,float x2,float y2){
		//public void drawLine2D(Point2D p1,Point2D p2){
			doPenPat(1);
			doFillPat(0);
			p.print("moveTo(");
			p.print(x1);
			p.print(",");
			p.print(y1);
			p.println(");");
			
			p.print("lineTo(");
			p.print(x2);
			p.print(",");
			p.print(y2);
			p.println(");");
		//}
	}
	public void noFill(){
		doFillPat(0);
		fill=false;
	}
	public void noStroke(){
		doPenPat(0);
		stroke=false;
	}
	public void strokeWeight(int m){
		// in millimeter
		p.println("PenSize("+m+");");
	}
	
	public void stroke(int r,int g, int b){
		stroke=true;
		doPenPat(1);
		String colorString="";
		colorString+=""+getIntVWColor(r);
		colorString+=","+getIntVWColor(g);
		colorString+=","+getIntVWColor(b);
		p.println("PenFore("+colorString+");");
	}
	public void fill(int r,int g, int b){
		fill=true;
		doFillPat(1);
		String colorString="";
		colorString+=""+getIntVWColor(r);
		colorString+=","+getIntVWColor(g);
		colorString+=","+getIntVWColor(b);
		// TODO Auto-generated method stub
		p.println("FillBack("+colorString+");");
	}
	public void nameClass(String name){
		p.println("nameClass('"+name+"');");
	}
	
	public void layer(String name){
		p.println("Layer('"+name+"');");
	}
	public void setName(String theNameToSet){
		p.println("SetName(LNewObj,"+ theNameToSet+");");
	}
	public void nameObject(String name){
		p.println("NameObject('"+name+"');");
	}
	
	public void dispose(){
		p.println("end;");
		p.println("run ("+scriptName+");");
		p.close();
		System.out.println("VSS export completed.");
	}
	private void doPenPat(int i){
		if (cPenPat!=i){
			cPenPat=i;
			p.println("PenPat("+cPenPat+");");
		}
	}
	private void doFillPat(int i){
		if (cFillPat!=i){
			cFillPat=i;
			p.println("FillPat("+cFillPat+");");
		}
	}
	private int getIntVWColor(int c){
		return (int)(c*65535.0/255.);
	}
	public void beginGroup(){
		p.println("beginGroup;");
	}
	public void endGroup(){
		p.println("endGroup;");
	}
	@Override
	public void text(String arg0, float arg1, float arg2) {
		// TODO Auto-generated method stub
		p.println("moveTo("+arg1+","+arg2+");");
		p.println("CreateText('"+arg0+"');");
		//super.text(arg0, arg1, arg2);
	}
}
